package MyRemoteSorter;
import java.util.Observable;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import MyRemoteSorter.server.*;

public class SorterThread extends Observable implements Runnable
{
	private Thread thread;
	private int threadNumber;
	private boolean loopt;
	private Comparable[] sorteerbaar;
	private Comparable[] gesorteerd;
	
	public SorterThread(Comparable[] compare, ListJoiner joiner)
	{
		sorteerbaar = compare;
		threadNumber = joiner.getNextThreadNumber();
		addObserver(joiner);
	}
	
	public int getThreadNumber()
	{
		return threadNumber;
	}
	
	public void run()
	{
		try {
			sort(sorteerbaar);
		} catch (Exception e) {
			e.printStackTrace();
		}
		setChanged();
		notifyObservers(gesorteerd);
		stop();
	}
	
	public void start() {
		thread = new Thread(this);
		thread.start();
	}

	public void stop() {
		thread = null;
	}
	
	public void sort(Comparable[] comparable) throws Exception 
	{		
		Registry registry = LocateRegistry.getRegistry(1099);
		ISortFactory sortFactory = (ISortFactory)registry.lookup("sortfactory");
		ISorter sorter = sortFactory.createSorter();
		gesorteerd = sorter.sort(comparable);			
	}
}
